package com.su.gulimall.ware.listener;

import com.alibaba.fastjson.TypeReference;
import com.rabbitmq.client.Channel;
import com.su.common.to.OrderTo;
import com.su.common.to.mq.StockDetailTo;
import com.su.common.to.mq.StockLockedTo;
import com.su.common.utils.R;
import com.su.gulimall.ware.dao.WareSkuDao;
import com.su.gulimall.ware.entity.WareOrderTaskDetailEntity;
import com.su.gulimall.ware.entity.WareOrderTaskEntity;
import com.su.gulimall.ware.feign.OrderFeignService;
import com.su.gulimall.ware.service.WareOrderTaskDetailService;
import com.su.gulimall.ware.service.WareOrderTaskService;
import com.su.gulimall.ware.service.WareSkuService;
import com.su.gulimall.ware.vo.OrderVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
//监听库存释放队列，有信息，就执行
@RabbitListener(queues = "stock.release.stock.queue")
public class StockReleaseListener {
    @Autowired
    WareSkuService wareSkuService;
    @RabbitHandler
    public void handleStockLockedRelease(StockLockedTo lockedTo, Message message, Channel channel) throws IOException { System.out.println("收到库存解锁的信息==============");
        //消息的手动确认
        try {
            wareSkuService.unlockStock(lockedTo);
            //消息消费成功，发送确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
        catch (Exception e) {
            //有异常就是解锁失败，将消息重新归队，等待下一次消费
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
    @RabbitHandler
    public void  handlerOrderCloseRelease(OrderTo orderTo, Message message, Channel channel) throws IOException {
        System.out.println("收到订单关闭的消息===========");
        try {
            wareSkuService.unlockStock(orderTo);
            //消息消费成功，发送确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
        catch (Exception e) {
            //有异常就是解锁失败，将消息重新归队，等待下一次消费
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }

}
